package edu.lit.B180401;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import edu.lit.B180401.service.Service;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_stuno;
    private EditText et_name;
    private CheckBox cb_run;
    private CheckBox cb_football;
    private CheckBox cb_swim;
    private LinearLayout habbys;
    private Button button;
    Service service = new Service();
    DBHelper helper;
    SQLiteDatabase db;
    private Button stulist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        helper = new DBHelper(this);
        //只在第一次运行插入数据
        /*service.insertHobbyType(this,"跑步");
        service.insertHobbyType(this,"足球");
        service.insertHobbyType(this,"游泳");*/
    }

    private void initView() {
        et_stuno = (EditText) findViewById(R.id.et_stuno);
        et_name = (EditText) findViewById(R.id.et_name);
        cb_run = (CheckBox) findViewById(R.id.cb_run);
        cb_football = (CheckBox) findViewById(R.id.cb_football);
        cb_swim = (CheckBox) findViewById(R.id.cb_swim);
        habbys = (LinearLayout) findViewById(R.id.habbys);
        button = (Button) findViewById(R.id.button);

        button.setOnClickListener(this);
        stulist = (Button) findViewById(R.id.stulist);
        stulist.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                submit();
                break;
            case R.id.stulist:
                Intent intent=new Intent(this,Show.class);
                startActivity(intent);
                break;
        }
    }

    private void submit() {
        // validate
        String stuno = et_stuno.getText().toString().trim();
        if (TextUtils.isEmpty(stuno)) {
            Toast.makeText(this, "stuno不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        String name = et_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "name不能为空", Toast.LENGTH_SHORT).show();
            return;
        }


        int count2 = habbys.getChildCount();
        String str_habbys[] = new String[count2];
        int checkCount = 0;
        for (int i = 0; i < count2; i++) {
            CheckBox checkBox = (CheckBox) habbys.getChildAt(i);
            if (checkBox.isChecked()) {
                str_habbys[checkCount] = checkBox.getText().toString();
                checkCount++;
            }
        }
        Student student = service.selectStudentByStunoAndName(this, stuno, name);
        if (student.getStuno() == null && student.getStuno() == null) {//添加信息


            service.insertStuInfo(this, name, stuno);
            Log.i("smh", "添加学生");
        } else {//修改信息
            service.delByStuid(this, service.selectStudentByStunoAndName(this, stuno, name).getId());
            Log.i("smh", "修改学生" + student);

        }
        for (int i = 0; i < checkCount; i++) {
            HobbyType hobbyType = service.selcetHobbyTypeByName(this, str_habbys[i]);
            service.insertHobbyInfo(this,
                    service.selectStudentByStunoAndName(this, stuno, name).getId(),
                    hobbyType.getId());
        }


        // TODO validate success, do something


    }
}
